package com.zygh.policeasset.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Images;
import com.zygh.policeasset.service.ImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping({"/oldUploadimages"})
@Api(tags = {"图片上传模块"}, description = "UploadFileController")
@RestController
@CrossOrigin
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/ImagesController.class */
public class ImagesController {
    @Autowired
    private ImagesService imagesService;

    @RequestMapping(value = {"/findbypuid"}, method = {RequestMethod.GET})
    @ApiOperation("获取某采购资产数据")
    public Object findById(@RequestParam int aid, @RequestParam String type) {
        return this.imagesService.findByAId(aid, type);
    }

    @RequestMapping(value = {"/image"}, method = {RequestMethod.POST})
    @ApiOperation("添加图片")
    public Object image(Images images) {
        if (StrUtil.isEmpty(images.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(images.getAssetsId()))) {
            return new CommonResult().validateFailed("采购资产id不能为空");
        }
        MultipartFile icon = images.getIcon();
        if (icon == null || "".equals(icon)) {
            return new CommonResult().validateFailed("图像不允许为空！请重新输入！");
        }
        if (icon.getSize() > 2097152) {
            return new CommonResult().validateFailed("文件大于2M！");
        }
        String header = icon.getOriginalFilename();
        if (!Arrays.asList(".jpg", ".png", ".gif", ".PNG").contains(header.substring(header.lastIndexOf("."), header.length()))) {
            return new CommonResult().validateFailed("文件格式错误！请重新输入！");
        }
        String imagesPath = this.imagesService.storeFile(images);
        images.setCreateDate(DateUtil.now());
        images.setConnect(imagesPath);
        if (this.imagesService.add(images).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("上传错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改图片")
    public Object update(Images images) {
        if (StrUtil.isEmpty(images.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(images.getAssetsId()))) {
            return new CommonResult().validateFailed("采购资产id不能为空");
        }
        MultipartFile icon = images.getIcon();
        if (icon == null || "".equals(icon)) {
            return new CommonResult().validateFailed("图像不允许为空！请重新输入！");
        }
        if (icon.getSize() > 2097152) {
            return new CommonResult().validateFailed("文件大于2M！");
        }
        String header = icon.getOriginalFilename();
        if (!Arrays.asList(".jpg", ".png", ".gif", ".PNG").contains(header.substring(header.lastIndexOf("."), header.length()))) {
            return new CommonResult().validateFailed("文件格式错误！请重新输入！");
        }
        images.setConnect(this.imagesService.storeFile(images));
        if (this.imagesService.update(images).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除图片")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.imagesService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }
}